<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="2正常用户 3禁言用户 4虚拟用户 5运营" prop="userRole">
      <el-input v-model="dataForm.userRole" placeholder="2正常用户 3禁言用户 4虚拟用户 5运营"></el-input>
    </el-form-item>
    <el-form-item label="注册来源：1手机号 2邮箱 3用户名 4qq 5微信 6腾讯微博 7新浪微博" prop="registerSource">
      <el-input v-model="dataForm.registerSource" placeholder="注册来源：1手机号 2邮箱 3用户名 4qq 5微信 6腾讯微博 7新浪微博"></el-input>
    </el-form-item>
    <el-form-item label="用户账号，必须唯一" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户账号，必须唯一"></el-input>
    </el-form-item>
    <el-form-item label="用户昵称" prop="nickName">
      <el-input v-model="dataForm.nickName" placeholder="用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="用户性别 0-female 1-male" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="用户性别 0-female 1-male"></el-input>
    </el-form-item>
    <el-form-item label="用户生日" prop="birthday">
      <el-input v-model="dataForm.birthday" placeholder="用户生日"></el-input>
    </el-form-item>
    <el-form-item label="手机号码(唯一)" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号码(唯一)"></el-input>
    </el-form-item>
    <el-form-item label="手机号码绑定时间" prop="mobileBindTime">
      <el-input v-model="dataForm.mobileBindTime" placeholder="手机号码绑定时间"></el-input>
    </el-form-item>
    <el-form-item label="邮箱(唯一)" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱(唯一)"></el-input>
    </el-form-item>
    <el-form-item label="邮箱绑定时间" prop="emailBindTime">
      <el-input v-model="dataForm.emailBindTime" placeholder="邮箱绑定时间"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="face">
      <el-input v-model="dataForm.face" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="头像 200x200x80" prop="face200">
      <el-input v-model="dataForm.face200" placeholder="头像 200x200x80"></el-input>
    </el-form-item>
    <el-form-item label="原图头像" prop="srcface">
      <el-input v-model="dataForm.srcface" placeholder="原图头像"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="用户push_token" prop="pushToken">
      <el-input v-model="dataForm.pushToken" placeholder="用户push_token"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          userRole: '',
          registerSource: '',
          userName: '',
          nickName: '',
          gender: '',
          birthday: '',
          mobile: '',
          mobileBindTime: '',
          email: '',
          emailBindTime: '',
          face: '',
          face200: '',
          srcface: '',
          createTime: '',
          updateTime: '',
          pushToken: '',
          deleted: ''
        },
        dataRule: {
          userRole: [
            { required: true, message: '2正常用户 3禁言用户 4虚拟用户 5运营不能为空', trigger: 'blur' }
          ],
          registerSource: [
            { required: true, message: '注册来源：1手机号 2邮箱 3用户名 4qq 5微信 6腾讯微博 7新浪微博不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户账号，必须唯一不能为空', trigger: 'blur' }
          ],
          nickName: [
            { required: true, message: '用户昵称不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '用户性别 0-female 1-male不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '用户生日不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号码(唯一)不能为空', trigger: 'blur' }
          ],
          mobileBindTime: [
            { required: true, message: '手机号码绑定时间不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱(唯一)不能为空', trigger: 'blur' }
          ],
          emailBindTime: [
            { required: true, message: '邮箱绑定时间不能为空', trigger: 'blur' }
          ],
          face: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          face200: [
            { required: true, message: '头像 200x200x80不能为空', trigger: 'blur' }
          ],
          srcface: [
            { required: true, message: '原图头像不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          pushToken: [
            { required: true, message: '用户push_token不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除（0.删除 1.未删除）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/userbase/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userRole = data.userBase.userRole
                this.dataForm.registerSource = data.userBase.registerSource
                this.dataForm.userName = data.userBase.userName
                this.dataForm.nickName = data.userBase.nickName
                this.dataForm.gender = data.userBase.gender
                this.dataForm.birthday = data.userBase.birthday
                this.dataForm.mobile = data.userBase.mobile
                this.dataForm.mobileBindTime = data.userBase.mobileBindTime
                this.dataForm.email = data.userBase.email
                this.dataForm.emailBindTime = data.userBase.emailBindTime
                this.dataForm.face = data.userBase.face
                this.dataForm.face200 = data.userBase.face200
                this.dataForm.srcface = data.userBase.srcface
                this.dataForm.createTime = data.userBase.createTime
                this.dataForm.updateTime = data.userBase.updateTime
                this.dataForm.pushToken = data.userBase.pushToken
                this.dataForm.deleted = data.userBase.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/userbase/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'userRole': this.dataForm.userRole,
                'registerSource': this.dataForm.registerSource,
                'userName': this.dataForm.userName,
                'nickName': this.dataForm.nickName,
                'gender': this.dataForm.gender,
                'birthday': this.dataForm.birthday,
                'mobile': this.dataForm.mobile,
                'mobileBindTime': this.dataForm.mobileBindTime,
                'email': this.dataForm.email,
                'emailBindTime': this.dataForm.emailBindTime,
                'face': this.dataForm.face,
                'face200': this.dataForm.face200,
                'srcface': this.dataForm.srcface,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'pushToken': this.dataForm.pushToken,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
